/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.khanhhoi;

/**
 *
 * @author Long
 */
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

public class YuiCompressor {

    public static void main(String args[]) {
        try {

        } catch (Exception e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
        }
    }

    private static Logger logger = Logger.getLogger(YuiCompressor.class.getName());

    private static class YuiCompressorErrorReporter implements ErrorReporter {

        public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
            if (line < 0) {
                logger.log(Level.WARNING, message);
            } else {
                logger.log(Level.WARNING, line + ':' + lineOffset + ':' + message);
            }
        }

        public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
            if (line < 0) {
                logger.log(Level.SEVERE, message);
            } else {
                logger.log(Level.SEVERE, line + ':' + lineOffset + ':' + message);
            }
        }

        public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset) {
            error(message, sourceName, line, lineSource, lineOffset);
            return new EvaluatorException(message);
        }
    }

    public static class Options {

        public String charset = "UTF-8";
        public int lineBreakPos = -1;
        public boolean munge = true;
        public boolean verbose = false;
        public boolean preserveAllSemiColons = false;
        public boolean disableOptimizations = false;
    }

    public static String compressJavaScript(String inputString, Options o) {

        Reader in = new StringReader(inputString);
        Writer out = new StringWriter();
        JavaScriptCompressor compressor;
        try {
            compressor = new JavaScriptCompressor(in, new YuiCompressorErrorReporter());
            in.close();
            compressor.compress(out, o.lineBreakPos, o.munge, o.verbose, o.preserveAllSemiColons, o.disableOptimizations);
        } catch (IOException ex) {
            Logger.getLogger(YuiCompressor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EvaluatorException ex) {
            Logger.getLogger(YuiCompressor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return out.toString();
    }
}
